package com.openrange.dukes.Dukes
{
	import com.openrange.dukes.*;
	import com.openrange.dukes.Dukes.*;
	import com.openrange.dukes.Input.*;
	import org.flixel.*;
	/**
	 * ...
	 * @author Dan Reid
	 */
	public class DukeManager 
	{
		
		private var _map:Terrain;
		private var _gui:GUI;
		
		private var _dukes:FlxGroup = new FlxGroup();
		
		private var _activeDuke:int = -1;
		
		public function DukeManager(noPlayers:int, noAI:int, map:Terrain, gui:GUI ) 
		{
			_map = map;
			_gui = gui;
			
			for (var ct:int = 0; ct < noPlayers; ct++)
			{
				AddPlayer();
			}
			for (ct = 0; ct < noAI; ct++)
			{
				AddAI();
			}
			NextDuke();
			
			FlxG.state.add(_dukes);
			
		}
		
		public function AddPlayer():void
		{
			_dukes.add(new Duke(_map.getValidStartingPosition(), new PlayerInput() , _map, _gui));
		}
		public function AddAI():void
		{
			_dukes.add(new Duke(_map.getValidStartingPosition(), new AiInput(), _map, _gui));
		}
		public function NextDuke():void
		{
			_activeDuke++;
			if (_activeDuke >= _dukes.length)
			{
				_activeDuke = 0;
			}
			for (var ct:int = 0; ct < _dukes.length; ct++)
			{
				_dukes.members[ct].deactivate();
			}
			_dukes.members[_activeDuke].activate();
		}
		
		public function Update():void
		{
			for (var ct:int = 0; ct < _dukes.length; ct++)
			{
				_dukes.members[ct].Update();
			}
			if (_dukes.members[_activeDuke].movementAllow == 0) { NextDuke(); } 
		}
		
	}

}